﻿@model QuanLyGiaoVuUI.Models.TaiLieu.CapNhatTaiLieuViewModel
@{
    Layout = null;
}

<style>
    .hiddenField {
        display: none;
    }

    #loadingImage {
        width: 100%;
        margin: 0 auto;
    }

        #loadingImage img {
            width: 100%;
            margin: 0 auto;
        }
</style>

<script src="@Url.Content("~/Scripts/jquery.form.js")"></script>
<input type="hidden" value="" id="filename" />

@{using (Ajax.BeginForm("TaoTaiLieu", "TaiLieuGiangVien", new AjaxOptions
      {
          HttpMethod = "POST",
          OnSuccess = "TaoTaiLieuThanhCong",
          LoadingElementId = "loadingImage",
          OnFailure = "TaoTaiLieuKhongThanhCong"
      }))
  { 
      @Html.HiddenFor(model => model.MaTaiLieu)
    <table border="0" cellpadding="0" cellspacing="0" id="id-form">
        <tr>
            <th valign="top">@Html.LabelFor(model => model.TenTaiLieu)</th>
            <td>@Html.TextBoxFor(model => model.TenTaiLieu)</td>
            <td>@Html.ValidationMessageFor(model => model.TenTaiLieu)</td>
        </tr>
        <tr>
            <th valign="top">@Html.LabelFor(model => model.MaLoaiTaiLieu)</th>
            <td>@Html.DropDownList("MaLoaiTaiLieu", new SelectList(Model.DanhSachLoaiTaiLieu, "MaLoaiTaiLieu", "TenLoaiTaiLieu", "\"" + ViewData["maThuMucCha"].ToString() + "\""))</td>
        </tr>
        <tr>
            <th valign="top">@Html.LabelFor(model => model.GhiChu)</th>
            <td>@Html.TextAreaFor(model => model.GhiChu)</td>
            <td>@Html.ValidationMessageFor(model => model.GhiChu)</td>
        </tr>
        <tr>
            <th valign="top">@Html.LabelFor(model => model.MaThuMuc)</th>
            <td>
                <input type="hidden" id="MaThuMuc" name="MaThuMuc" value="@ViewData["maThuMucCha"].ToString()" />
                <input type="text" id="DuongDan" name="DuongDan" value="@ViewData["duongDanThuMuc"].ToString()" readonly="readonly"/>
            </td>
        </tr>
        <tr>
            <input type="hidden" id="MaTapTin" name="MaTapTin" value="@((Model.MaTapTin == null || Model.MaTapTin.Equals(Guid.Empty)) ? String.Empty : Model.MaTapTin.ToString())" />
            <input type="hidden" id="KichThuoc" name="KichThuoc" value="@((Model.KichThuoc == null) ? String.Empty : Model.KichThuoc.ToString())" />
            <input type="hidden" id="TenTapTin" name="TenTapTin" value="@(Model.TenTapTin + Model.PhanMoRong)" />
        </tr>
    </table>
    
    <input type="submit" id="submitTaoTaiLieu" value="" class="hiddenField" />
  }
}
    @if (Model.MaTaiLieu == null || Model.MaTaiLieu.Equals(Guid.Empty))
    {
        <form id="uploadFile" name="UploadFile" action="/TaiLieu/UploadFile" method="post">
    <table>
        <tr>
            <th valign="top">File tài liệu:</th>
            <td>
                <input type="file" name="fileTaiLieu" id="#file" />
                @Html.ValidationMessageFor(model => model.MaTapTin)
            </td>
        </tr>
    </table>
    <div id="loadingImage" class="hiddenField ui-overlay">
        <img src="~/Images/gif/loading-bar.gif" /></div>

    <input type="submit" id="uploadFileSubmit" style="display: none;" />
</form>
    }
    else{
        <table>
        <tr>
            <th valign="top">File tài liệu:</th>
            <td>
                @Html.ActionLink("Tải file","TaiFile","TaiLieuGiangVien", new { fileid = Model.MaTapTin},null)
            </td>
        </tr>
    </table>
    }


<script>
    $(document).ready(function () {
        $("#uploadFile").ajaxForm({
            dataType: 'json',
            success: uploadFileSuccess,
            beforeSubmit: beforeUploadFile,
            error: uploadFileFail,
            uploadProgress: showProgress,
        });

        $("input:file").change(function () {
            $("#filename").val($(this).val());
        });

        $("#MyForm").submit(function () {
            if ($("#MyForm").valid()) {
                return true;
            }
            return false;
        });

        $("#DuongDan").click(function () {
            $("#chonThuMucDialog").dialog("open");
        });
    });

    function uploadFileSuccess(data) {
        $("#loadingImage").addClass("hiddenField");
        $("#MaTapTin").val(data.fileId);
        $("#TenTapTin").val(data.fileName);
        $("#KichThuoc").val(data.fileSize);
        $("#submitTaoTaiLieu").click();
    }

    function uploadFileFail() {
        $("#loadingImage").addClass("hiddenField");
        alert("Không thể upload file.");
    }

    function showProgress(event, position, total, percentComplete) {
        $("#position").val(position);
    }

    function SubmitTaoTaiLieu() {
        if ($("#filename").val().length > 0)
            $("#uploadFileSubmit").click();
        else
            alert("Chưa chọn file.");
    }

        function beforeUploadFile() {
            $("#loadingImage").removeClass("hiddenField");
        }
</script>
